<?php
/**
 * Created by PhpStorm.
 * User: zk
 * Date: 2018/9/27
 * Time: 下午3:48
 */

namespace app\models;
use yii\db\ActiveRecord;

class Gl_report extends ActiveRecord {
    //获取举报列表
    public  function getReportList($limit, $offset, $startTime, $endTime, $reportType, $cityCode, $reportStatus) {
        $post = Gl_report::find()->JoinWith('user');
        if ($reportStatus != -1) {
            $post->andWhere(['gl_report.is_deleted' => $reportStatus]);
        }

        if ($startTime != -1) {
            $post->andWhere(['between', 'gl_report.gmt_create', $startTime, $endTime]);
        }

        if ($reportType != -1) {
            $post->andWhere(['report_type' => $reportType]);
        }

        if ($cityCode != -1) {
            $post->andWhere(['user_user.city_code' => $cityCode]);
        }

        $count = $post->count();
        $result = $post->orderBy('report_id DESC')->limit($limit)->offset($offset)->asArray()->all();
        return ['total'=> $count, 'rows'=>$result];
    }

    // 获取举报人姓名
    public function getUser()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(User_user::className(), ['user_id' => 'user_id']);
    }

    //设置举报状态
    public function updateReport($reportId, $isDeleted) {
        $post = Gl_report::findOne(['report_id' => $reportId]);
        $post->is_deleted = $isDeleted;
        return $post->save();
    }

//    查找举报目标id
    public function findTarget ($reportId)
    {
        $result = Gl_report::find()->where(['report_id' => $reportId])->asArray()->one();

        return $result;
    }

}